{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#-*- coding: utf-8 -*-"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 函数的定义及调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#函数的定义及调用\n",
    "#函数三要素：参数、函数体、返回值\n",
    "# 函数定义 求正方形的面积\n",
    "def area_of_square(length_of_side):\n",
    "    square_area = pow(length_of_side,2)\n",
    "    return square_area"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "25\n"
     ]
    }
   ],
   "source": [
    "#函数调用  函数名(参数)\n",
    "area = area_of_square(5)\n",
    "print (area)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 3\n"
     ]
    }
   ],
   "source": [
    "#参数的传递\n",
    "#形参（形式参数）：函数定义时的参数，实际上就是变量名\n",
    "#实参（实际参数）：函数调用时的参数，实际上就是变量的值\n",
    "\n",
    "#位置参数 严格按照位置顺序，用实参对形参进行赋值（关联） 一般用在参数比较少的时候\n",
    "#形参与实参必须一一对应，一个不能多，一个不能少\n",
    "def function(x,y,z):\n",
    "    print (x,y,z)\n",
    "    \n",
    "function(1,2,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3 1 2\n"
     ]
    }
   ],
   "source": [
    "#关键字参数\n",
    "#打破位置限制，直呼其名的进行值的传递(形参=实参）\n",
    "#必须遵守实参与形参数量上的一一对应 多用在参数比较多的场合\n",
    "def function1(x,y,z):\n",
    "    print(x,y,z)\n",
    "function1(y = 1, z = 2, x = 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 3 2\n"
     ]
    }
   ],
   "source": [
    "#位置参数可以与关键字参数混合使用，但是位置参数必须放在关键字参数前面\n",
    "function1(1,z=2,y=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "#不能为同一个形参重复传值\n",
    "def function2(x,y,z):\n",
    "    print(x,y,z)\n",
    "#function2(1, z=2, x = 3) 报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "andy 18 male\n"
     ]
    }
   ],
   "source": [
    "#默认参数 在定义阶段就给形参赋值 -- 该形参的常用值\n",
    "#机器学习库中类的方法里非常常见 调用函数时，可以不对该参数传值\n",
    "def register(name,age,sex=\"male\"):\n",
    "    print(name,age,sex)\n",
    "register(\"andy\",18)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "linzhiling 38 female\n"
     ]
    }
   ],
   "source": [
    "#也可以按正常的形参进行传值\n",
    "register(\"linzhiling\",38,\"female\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4429316360\n",
      "4429316360\n",
      "[1]\n",
      "4429316360\n",
      "4429316360\n",
      "[1, 1]\n",
      "4429316360\n",
      "4429316360\n",
      "[1, 1, 1]\n"
     ]
    }
   ],
   "source": [
    "#默认参数应该设置为不可变类型(数字、字符串、元组)\n",
    "#不能是可变类型，比如如下所示的列表，列表的地址不发生变化，具有记忆功能\n",
    "# 但是它里面的内容会发生变化\n",
    "def function3(ls=[]):\n",
    "    print (id(ls)) #打印ls列表的地址\n",
    "    ls.append(1)\n",
    "    print (id(ls))\n",
    "    print (ls)\n",
    "\n",
    "function3()\n",
    "function3()\n",
    "function3()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4332805568\n",
      "4429319664\n",
      "Python3.7\n",
      "4332805568\n",
      "4429319856\n",
      "Python3.7\n",
      "4332805568\n",
      "4429320048\n",
      "Python3.7\n"
     ]
    }
   ],
   "source": [
    "#同一个函数我们把形参改成字符串,我们可以看到每次地址都发生来变化，无记忆功能\n",
    "def function4(ls=\"Python\"):\n",
    "    print (id(ls))\n",
    "    ls += \"3.7\"\n",
    "    print (id(ls))\n",
    "    print (ls)\n",
    "\n",
    "function4()\n",
    "function4()\n",
    "function4()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dazai\n",
      "dajiezai\n"
     ]
    }
   ],
   "source": [
    "#把不确定是否会使用的参数设置成None，让参数变成可选的\n",
    "def name(first_name,last_name,middle_name=None):\n",
    "    if middle_name:\n",
    "        return first_name+middle_name+last_name\n",
    "    else:\n",
    "        return first_name+last_name\n",
    "\n",
    "print(name(\"da\",\"zai\"))\n",
    "print(name(\"da\",\"zai\",\"jie\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 3\n",
      "(4, 5, 6)\n"
     ]
    }
   ],
   "source": [
    "#包.可变长参数 *args\n",
    "#不知道会传过来多少参数 *args 该形参必须放在参数列表道最后\n",
    "def foo(x,y,z,*args):\n",
    "    print (x,y,z)\n",
    "    print (args)\n",
    "\n",
    "foo(1,2,3,4,5,6)  #多余道参数，打包传递给args"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 3\n",
      "([4, 5, 6],)\n",
      "1 2 3\n",
      "(4, 5, 6)\n"
     ]
    }
   ],
   "source": [
    "#实参打散\n",
    "def foo1(x,y,z,*args):\n",
    "    print (x,y,z)\n",
    "    print (args)\n",
    "foo1(1,2,3,[4,5,6]) #[包,5,6]这个列表作为作为元组道一个元素而存在\n",
    "foo1(1,2,3,*[4,5,6]) #将列表中道元组打散，作为一个个单独的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 3\n",
      "{'a': 4, 'b': 5, 'c': 6}\n"
     ]
    }
   ],
   "source": [
    "#可变长参数 **kwargs 它的用法和字典有关\n",
    "def foo2(x,y,z, **kwargs):\n",
    "    print(x,y,z)\n",
    "    print(kwargs)\n",
    "\n",
    "foo2(1,2,3, a=4,b=5,c=6)#多余的参数，以字典的形式打包传递给kwargs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 3\n",
      "{'a': 4, 'b': 5, 'c': 6}\n"
     ]
    }
   ],
   "source": [
    "#字典实参打散\n",
    "def foo3(x,y,z,**kwargs):\n",
    "    print (x,y,z)\n",
    "    print (kwargs)\n",
    "\n",
    "foo3(1,2,3,**{\"a\":4,\"b\":5,\"c\":6})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2, 3)\n",
      "{'a': 4, 'b': 5, 'c': 6}\n"
     ]
    }
   ],
   "source": [
    "#可变长参数的组合使用\n",
    "def foo4(*args,**kwargs):\n",
    "    print(args)\n",
    "    print(kwargs)\n",
    "\n",
    "foo4(1,2,3,a=4,b=5,c=6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "18"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#函数体与变量的作用域\n",
    "#函数体就是一段只在函数被调用时，才会执行的代码，代码构成与其他代码并无不同\n",
    "#局部变量   仅在函数体内定义和发挥作用\n",
    "def multipy(x,y):\n",
    "    z = x*y\n",
    "    return z\n",
    "\n",
    "multipy(2,9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "54\n"
     ]
    }
   ],
   "source": [
    "#print (z) 这里报错，因为函数执行完毕，局部变量z已经被释放掉了\n",
    "#全局变量  外部定义的都是全局变量 全局变量可以在函数体直接使用\n",
    "n = 3\n",
    "ls = [0]\n",
    "def multify1(x,y):\n",
    "    z = n*x*y\n",
    "    ls.append(z)\n",
    "    return z\n",
    "print (multify1(2,9))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18\n"
     ]
    }
   ],
   "source": [
    "#通过global在函数体内定义全局变量\n",
    "def multipy2(x,y):\n",
    "    global c\n",
    "    c = x*y\n",
    "    return c\n",
    "\n",
    "multipy2(2,9)\n",
    "print (c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "#返回值\n",
    "#单个返回值\n",
    "def foo5(x):\n",
    "    return x**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 3, 9, 27)\n",
      "1 3 9 27\n"
     ]
    }
   ],
   "source": [
    "#多个返回值 -- 以元组的形式\n",
    "def foo6(x = 1):\n",
    "    return 1,x,x**2,x**3\n",
    "print (foo6(3))\n",
    "a,b,c,d = foo6(3)  #解包赋值\n",
    "print (a,b,c,d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Is holiday\n",
      "Not holiday\n"
     ]
    }
   ],
   "source": [
    "#可以有多个return语句，一旦其中一个执行，代表了函数运行的结束\n",
    "def is_holidy(day):\n",
    "    if day in [\"Sunday\",\"Saturday\"]:\n",
    "        return \"Is holiday\"\n",
    "    else:\n",
    "        return \"Not holiday\"\n",
    "    print (\"lalalala damaxiya\") #没机会运行\n",
    "\n",
    "print(is_holidy(\"Sunday\"))\n",
    "print(is_holidy(\"Monday\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我是孙悟空\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "#没有return语句，返回值为None\n",
    "def foo7():\n",
    "    print(\"我是孙悟空\")\n",
    "\n",
    "res = foo7()\n",
    "print (res)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 函数式编程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "#我们用一个例子解释一下什么是函数式编程\n",
    "#小A和小B进行比赛，小A每球获胜的概率为55%，小B每球获胜的概率为45%，每局比赛，先赢21球者获胜\n",
    "#假设进行 n=10000局独立的比赛，小A会获胜多少?(n较大但时候，实验结果≈真实期望)\n",
    "\n",
    "def main():\n",
    "    #主要逻辑\n",
    "    prob_A, prob_B, number_of_games = get_inputs() #获取原始数据，小A每球获胜概率，小B每球获胜概率，比赛场次\n",
    "    win_A,win_B = sim_n_games(prob_A,prob_B,number_of_games) #获取小A赢了多少场，小B赢了多少场\n",
    "    #print_summary(win_A,win_B,number_of_games)  #将获取但结果打印输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "#输入原始数据\n",
    "def get_inputs():\n",
    "    #输入原始数据\n",
    "    prob_A = input('请输入运动员A的每球获胜概率：')\n",
    "    prob_B = round(1-prob_A,2)\n",
    "    number_of_gamgs = input(\"请输入模拟但场次（正整数): \")\n",
    "    # print(\"模拟比赛总次数：\",number_of_gamgs)\n",
    "    # print(\"A 选手每球获胜概率：\",prob_A)\n",
    "    # print(\"B 选手每球获胜概率：\",prob_B)\n",
    "    return prob_A,prob_B,number_of_gamgs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入运动员A的每球获胜概率：\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for -: 'int' and 'str'",
     "output_type": "error",
     "traceback": [
      "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[0;31mTypeError\u001B[0m                                 Traceback (most recent call last)",
      "\u001B[0;32m<ipython-input-41-62a4c935c041>\u001B[0m in \u001B[0;36m<module>\u001B[0;34m()\u001B[0m\n\u001B[1;32m      1\u001B[0m \u001B[0;31m#单元测试\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m----> 2\u001B[0;31m \u001B[0mprob_A\u001B[0m\u001B[0;34m,\u001B[0m\u001B[0mprob_B\u001B[0m\u001B[0;34m,\u001B[0m\u001B[0mnumber_of_games\u001B[0m \u001B[0;34m=\u001B[0m \u001B[0mget_inputs\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m\u001B[1;32m      3\u001B[0m \u001B[0;31m#print (prob_A,prob_B,number_of_games)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n",
      "\u001B[0;32m<ipython-input-39-e51a0cf57aa5>\u001B[0m in \u001B[0;36mget_inputs\u001B[0;34m()\u001B[0m\n\u001B[1;32m      3\u001B[0m     \u001B[0;31m#输入原始数据\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m      4\u001B[0m     \u001B[0mprob_A\u001B[0m \u001B[0;34m=\u001B[0m \u001B[0minput\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;34m'请输入运动员A的每球获胜概率：'\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m----> 5\u001B[0;31m     \u001B[0mprob_B\u001B[0m \u001B[0;34m=\u001B[0m \u001B[0mround\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;36m1\u001B[0m\u001B[0;34m-\u001B[0m\u001B[0mprob_A\u001B[0m\u001B[0;34m,\u001B[0m\u001B[0;36m2\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m\u001B[1;32m      6\u001B[0m     \u001B[0mnumber_of_gamgs\u001B[0m \u001B[0;34m=\u001B[0m \u001B[0minput\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;34m\"请输入模拟但场次（正整数): \"\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m      7\u001B[0m     \u001B[0;31m# print(\"模拟比赛总次数：\",number_of_gamgs)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n",
      "\u001B[0;31mTypeError\u001B[0m: unsupported operand type(s) for -: 'int' and 'str'"
     ]
    }
   ],
   "source": [
    "#单元测试\n",
    "prob_A,prob_B,number_of_games = get_inputs()\n",
    "#print (prob_A,prob_B,number_of_games)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#多场比赛模拟\n",
    "def sim_n_games(prob_A,prob_B,number_of_gmaes):\n",
    "    #模拟多场比赛但结果\n",
    "    win_A,win_B = 0,0  #初始化A，B获胜但场次\n",
    "    for i in range(number_of_games):\n",
    "        score_A, score_B = sim_one_game(prob_A,prob_B) #获得模拟一次比赛但比分\n",
    "        if score_A > score_B:\n",
    "            win_A += 1\n",
    "        else:\n",
    "            win_B += 1\n",
    "\n",
    "    return win_A,win_B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "def sim_one_game(prob_A,prob_B):\n",
    "    #模拟一场比赛但结果\n",
    "    score_A,score_B = 0,0\n",
    "    while not game_over(score_A,score_B):\n",
    "        if random.random() < prob_A:\n",
    "            score_A += 1\n",
    "        else:\n",
    "            score_B += 1\n",
    "    return score_A,score_B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def game_over(score_A,score_B):\n",
    "    return  score_A == 21 or score_B == 21"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'game_over' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[0;31mNameError\u001B[0m                                 Traceback (most recent call last)",
      "\u001B[0;32m<ipython-input-42-ca8868f1010d>\u001B[0m in \u001B[0;36m<module>\u001B[0;34m()\u001B[0m\n\u001B[1;32m      1\u001B[0m \u001B[0;31m#单元测试 用 asser---断言\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m      2\u001B[0m \u001B[0;31m#表达式结果为false但时候触发异常\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m----> 3\u001B[0;31m \u001B[0;32massert\u001B[0m \u001B[0mgame_over\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;36m21\u001B[0m\u001B[0;34m,\u001B[0m\u001B[0;36m8\u001B[0m\u001B[0;34m)\u001B[0m \u001B[0;34m==\u001B[0m \u001B[0;32mTrue\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m",
      "\u001B[0;31mNameError\u001B[0m: name 'game_over' is not defined"
     ]
    }
   ],
   "source": [
    "#单元测试 用 asser---断言\n",
    "#表达式结果为false但时候触发异常\n",
    "assert game_over(21,8) == True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入运动员A的每球获胜概率：\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for -: 'int' and 'str'",
     "output_type": "error",
     "traceback": [
      "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[0;31mTypeError\u001B[0m                                 Traceback (most recent call last)",
      "\u001B[0;32m<ipython-input-43-89026442c8a6>\u001B[0m in \u001B[0;36m<module>\u001B[0;34m()\u001B[0m\n\u001B[1;32m      1\u001B[0m \u001B[0;32mif\u001B[0m \u001B[0m__name__\u001B[0m \u001B[0;34m==\u001B[0m \u001B[0;34m\"__main__\"\u001B[0m\u001B[0;34m:\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m----> 2\u001B[0;31m     \u001B[0mmain\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m",
      "\u001B[0;32m<ipython-input-27-1ce3d136518a>\u001B[0m in \u001B[0;36mmain\u001B[0;34m()\u001B[0m\n\u001B[1;32m      5\u001B[0m \u001B[0;32mdef\u001B[0m \u001B[0mmain\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m:\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m      6\u001B[0m     \u001B[0;31m#主要逻辑\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m----> 7\u001B[0;31m     \u001B[0mprob_A\u001B[0m\u001B[0;34m,\u001B[0m \u001B[0mprob_B\u001B[0m\u001B[0;34m,\u001B[0m \u001B[0mnumber_of_games\u001B[0m \u001B[0;34m=\u001B[0m \u001B[0mget_inputs\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;34m)\u001B[0m \u001B[0;31m#获取原始数据，小A每球获胜概率，小B每球获胜概率，比赛场次\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m\u001B[1;32m      8\u001B[0m     \u001B[0mwin_A\u001B[0m\u001B[0;34m,\u001B[0m\u001B[0mwin_B\u001B[0m \u001B[0;34m=\u001B[0m \u001B[0msim_n_games\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0mprob_A\u001B[0m\u001B[0;34m,\u001B[0m\u001B[0mprob_B\u001B[0m\u001B[0;34m,\u001B[0m\u001B[0mnumber_of_games\u001B[0m\u001B[0;34m)\u001B[0m \u001B[0;31m#获取小A赢了多少场，小B赢了多少场\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m      9\u001B[0m     \u001B[0;31m#print_summary(win_A,win_B,number_of_games)  #将获取但结果打印输出\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n",
      "\u001B[0;32m<ipython-input-39-e51a0cf57aa5>\u001B[0m in \u001B[0;36mget_inputs\u001B[0;34m()\u001B[0m\n\u001B[1;32m      3\u001B[0m     \u001B[0;31m#输入原始数据\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m      4\u001B[0m     \u001B[0mprob_A\u001B[0m \u001B[0;34m=\u001B[0m \u001B[0minput\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;34m'请输入运动员A的每球获胜概率：'\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m----> 5\u001B[0;31m     \u001B[0mprob_B\u001B[0m \u001B[0;34m=\u001B[0m \u001B[0mround\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;36m1\u001B[0m\u001B[0;34m-\u001B[0m\u001B[0mprob_A\u001B[0m\u001B[0;34m,\u001B[0m\u001B[0;36m2\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m\u001B[1;32m      6\u001B[0m     \u001B[0mnumber_of_gamgs\u001B[0m \u001B[0;34m=\u001B[0m \u001B[0minput\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;34m\"请输入模拟但场次（正整数): \"\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m      7\u001B[0m     \u001B[0;31m# print(\"模拟比赛总次数：\",number_of_gamgs)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n",
      "\u001B[0;31mTypeError\u001B[0m: unsupported operand type(s) for -: 'int' and 'str'"
     ]
    }
   ],
   "source": [
    "if __name__ == \"__main__\":\n",
    "    main()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 匿名函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(76, 94), (79, 100), (85, 85), (86, 71), (93, 88)]\n"
     ]
    }
   ],
   "source": [
    "#匿名函数 基本形式 lambda 变量：函数体\n",
    "#常用用法 在参数列表中最适合使用匿名函数 尤其是 key = 搭配\n",
    "#排序sort()  sorted()\n",
    "ls = [(93,88),(79,100),(86,71),(85,85),(76,94)] #默认根据每一个元组的第一个元素进行升序排列\n",
    "ls.sort()\n",
    "print(ls)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(86, 71), (85, 85), (93, 88), (76, 94), (79, 100)]\n"
     ]
    }
   ],
   "source": [
    "#如果我们想要根据每个元组但第二个元素进行排序，则可以使用匿名函数完成\n",
    "ls.sort(key = lambda x:x[1]) #按照key这个元素设置的量进行排序，每个元素x也就是可迭代对象里的单个元素，根据每个元素的第一个位置(第2个元素)的值进行排序\n",
    "print (ls)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(93, 88), (79, 100), (85, 85), (76, 94), (86, 71)]\n"
     ]
    }
   ],
   "source": [
    "ls1 = [(93,88),(79,100),(86,71),(85,85),(76,94)]\n",
    "temp = sorted(ls,key=lambda x:x[0]+x[1],reverse=True) #根据总成绩进行排序\n",
    "print (temp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(79, 100)\n",
      "(86, 71)\n"
     ]
    }
   ],
   "source": [
    "#max() min()\n",
    "ls2 = [(93,88),(79,100),(86,71),(85,85),(76,94)]\n",
    "n = max(ls,key=lambda x:x[1]) #找到第二个元素的最大值\n",
    "print (n)\n",
    "n = min(ls,key=lambda x:x[1]) #找到第二个元素的最小值\n",
    "print (n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "============面向对象和面向过程=============\n"
     ]
    }
   ],
   "source": [
    "print (\"============面向对象和面向过程=============\")\n",
    "#面向过程 已过程为中心的编程思想，以“什么正在发生”为主要目标进行编程。 冰冷的 程序化的\n",
    "#面向对象  将现实生活中的事物抽象成对象，更关注\"谁在受影响\"，更加贴近现实 有血有肉，拟人的（物）化的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}